package Test2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class UDPSender {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(8888);

        //准备数据：从控制台读取System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String msg = reader.readLine();
            byte[] data = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(data, 0, data.length, new InetSocketAddress("localhost", 6060));

            //发送数据
            socket.send(packet);

            //断开连接条件
            if (msg.equals("bye")) break;
        }
        //关闭连接
        socket.close();
    }
}
